package com.qf.personal.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.personal.entiy.Out;
import com.qf.personal.entiy.Things;
import com.qf.personal.mapper.IndexMapper;
import com.qf.personal.mapper.OutMapper;
import com.qf.personal.service.OutService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class OutMapperImpl implements OutService {
    @Resource
    private OutMapper outMapper;
    @Resource
    private IndexMapper indexMapper;


    @Transactional
    public RespResult<String> add(Out out, int mId, int eId, int tId) {
        Long snow = IdWorker.getId();
        out.setOutId(snow);
        out.setOutLink(2);
        int a = outMapper.addManagement(out);
        Things things = new Things();
        try {
            if (a != 0) {
                outMapper.addCorrelation(snow, mId, eId, tId);

                return RespResult.success("新增成功");
            }
        } catch (ServiceException se) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return null;
    }

    @Override
    @Transactional
    public RespResult<String> deleteById(Long id) {
        int a = outMapper.delete(id);
        try {
            if (a == 1) {
                int b = indexMapper.delete(id);
                if (b == 1) {
                    return RespResult.success("删除成功");
                }
            }
        } catch (ServiceException se) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return null;
    }
}
